package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjStressRequiresBo;
import com.ruoyi.zhgd.domain.vo.ZjStressRequiresVo;
import com.ruoyi.zhgd.domain.ZjStressRequires;
import com.ruoyi.zhgd.mapper.ZjStressRequiresMapper;
import com.ruoyi.zhgd.service.IZjStressRequiresService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 压力机技术要求Service业务层处理
 *
 * @author cj
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjStressRequiresServiceImpl implements IZjStressRequiresService {

    private final ZjStressRequiresMapper baseMapper;

    /**
     * 查询压力机技术要求
     */
    @Override
    public ZjStressRequiresVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询压力机技术要求列表
     */
    @Override
    public TableDataInfo<ZjStressRequiresVo> queryPageList(ZjStressRequiresBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjStressRequires> lqw = buildQueryWrapper(bo);
        Page<ZjStressRequiresVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压力机技术要求列表
     */
    @Override
    public List<ZjStressRequiresVo> queryList(ZjStressRequiresBo bo) {
        LambdaQueryWrapper<ZjStressRequires> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjStressRequires> buildQueryWrapper(ZjStressRequiresBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjStressRequires> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSymc()), ZjStressRequires::getSymc, bo.getSymc());
        lqw.eq(StringUtils.isNotBlank(bo.getQddj()), ZjStressRequires::getQddj, bo.getQddj());
        lqw.eq(bo.getPressureValue() != null, ZjStressRequires::getPressureValue, bo.getPressureValue());
        return lqw;
    }

    /**
     * 新增压力机技术要求
     */
    @Override
    public R insertByBo(ZjStressRequiresBo bo) {
        ZjStressRequires add = BeanUtil.toBean(bo, ZjStressRequires.class);
        validEntityBeforeSave(add);
        List<Map> list = baseMapper.selectInList(bo);
        if (list.size() > 0) {
            return R.warn("技术要求已存在，禁止重复添加！");
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return R.ok(flag);
    }

    /**
     * 修改压力机技术要求
     */
    @Override
    public R updateByBo(ZjStressRequiresBo bo) {
        ZjStressRequires update = BeanUtil.toBean(bo, ZjStressRequires.class);
        validEntityBeforeSave(update);
        List<Map> list = baseMapper.selectUpList(bo);
        if (list.size() > 0) {
            return R.warn("技术要求已存在，禁止修改！");
        }
        boolean flag = baseMapper.updateById(update) > 0;
        return R.ok(flag);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjStressRequires entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压力机技术要求
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Float selectRequires(String symc, String qddj) {
        return baseMapper.selectRequires(symc,qddj);
    }
}
